#region using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.Messaging;
#endregion

namespace MapiApplication
{
    public partial class ProfileSettings : Form
    {
        #region Globals
        MapiManager mapiManager = new MapiManager();
        #endregion

        #region Constructors
        public ProfileSettings()
        {
            InitializeComponent();
            InitializePowerSettingsForm();
        }
        #endregion

        #region Helper Methods
        private void InitializePowerSettingsForm()
        {
            RenderAllProfilesIntoListBox();
            RenderAllProfilesIntoComboBox();
            if (lbxAvailableProfiles.Items.Count != 0)
            {
                this.btnRename.Enabled = true;
                this.btnRemove.Enabled = true;
                this.btnCopy.Enabled = true;
            }
        }

        private void RenderAllProfilesIntoComboBox()
        {
            List<string> availableProfiles = new List<string>();
            try
            {
                cmbxGetAllProfiles.Items.Clear();
                availableProfiles = mapiManager.GetAvailableProfiles();
                foreach (string profile in availableProfiles)
                    cmbxGetAllProfiles.Items.Add(profile);
                //TODO: Known Issue: GetDefaultProfile is not implemented yet.
                //cmbxGetAllProfiles.Text = mapiManager.GetDefaultProfile();
                cmbxGetAllProfiles.SelectedIndex = 0;
            }

            catch (MapiManagerException ex)
            {
                MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            catch (NotImplementedException ex)
            {
                MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }  

        private void RenderAllProfilesIntoListBox()
        {
            List<string> availableProfiles = new List<string>();
            try
            {
                lbxAvailableProfiles.Items.Clear();          
                availableProfiles = mapiManager.GetAvailableProfiles();
                foreach (string profile in availableProfiles)
                    lbxAvailableProfiles.Items.Add(profile);            
            }

            catch (MapiManagerException ex)
            {
                MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public string lbxBoxSelectedValue
        {
            get
            {
                return this.lbxAvailableProfiles.SelectedItem.ToString();
            }
        }

        private void SetProfile()
        {
            try
            {
                if (cmbxGetAllProfiles.Text != string.Empty)
                {
                    mapiManager.SetDefaultProfile(cmbxGetAllProfiles.Text);
                    Main.profileName = cmbxGetAllProfiles.Text;
                }
            }

            catch (MapiManagerException ex)
            {
                MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Event Handlers
        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddProfile addProfile = new AddProfile();
            DialogResult dgResult = addProfile.ShowDialog();

            if (dgResult == DialogResult.OK)
            {
                try
                {
                    mapiManager.CreateNewProfile(addProfile.TbxBoxValue);
                    RenderAllProfilesIntoListBox();
                    RenderAllProfilesIntoComboBox();
                    addProfile.Dispose();
                }

                catch (MapiManagerException ex)
                {
                    MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                catch (ArgumentNullException ex)
                {
                    MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                catch (ArgumentException ex)
                {
                    MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                addProfile.Dispose();
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            if (lbxAvailableProfiles.SelectedItem != null)
            {
                CopyProfile copyProfile = new CopyProfile(lbxAvailableProfiles.SelectedItem.ToString());
                DialogResult dgResult = copyProfile.ShowDialog();

                if (dgResult == DialogResult.OK)
                {
                    try
                    {
                        mapiManager.CopyProfile(lbxAvailableProfiles.SelectedItem.ToString(), copyProfile.TbxBoxValue.ToString());
                        RenderAllProfilesIntoListBox();
                        RenderAllProfilesIntoComboBox();
                        copyProfile.Dispose();
                    }

                    catch (MapiManagerException ex)
                    {
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    catch (ArgumentNullException ex)
                    {
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    catch (ArgumentException ex)
                    {
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else if (dgResult == DialogResult.Cancel)
                {
                    copyProfile.Dispose();
                }
            }
            else
            {
                MessageBox.Show("Please select a profile to copy.", "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnApplyProfile_Click(object sender, EventArgs e)
        {
            SetProfile();
            btnCancelProfile.Enabled = false;
            btnApplyProfile.Enabled = false;
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                mapiManager.DeleteProfile(lbxAvailableProfiles.SelectedItem.ToString());
                RenderAllProfilesIntoListBox();
                RenderAllProfilesIntoComboBox();
            }

            catch (MapiManagerException ex)
            {
                MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cmbxGetAllProfiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btnApplyProfile.Enabled = true;
        }

        private void ProfileSettings_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private void btnCancelProfile_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOkProfile_Click(object sender, EventArgs e)
        {
            SetProfile();
            this.Close();
        }
        #endregion   

        private void btnRename_Click(object sender, EventArgs e)
        {
            if (lbxAvailableProfiles.SelectedItem != null)
            {
                RenameProfile renameProfile = new RenameProfile(lbxAvailableProfiles.SelectedItem.ToString());
                DialogResult dgResult = renameProfile.ShowDialog();

                if (dgResult == DialogResult.OK)
                {
                    try
                    {
                        mapiManager.RenameProfile(lbxAvailableProfiles.SelectedItem.ToString(), renameProfile.TbxBoxValue.ToString());
                        RenderAllProfilesIntoListBox();
                        RenderAllProfilesIntoComboBox();
                        renameProfile.Dispose();
                    }

                    catch (MapiManagerException ex)
                    {
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    catch (ArgumentNullException ex)
                    {
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    catch (ArgumentException ex)
                    {
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else if (dgResult == DialogResult.Cancel)
                {
                    renameProfile.Dispose();
                }
            }
            else
            {
                MessageBox.Show("Please select a profile to rename.", "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}